package com.lt.bookkeeping.bookActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;
import com.lt.bookkeeping.Fragment.FragmentDataStatistic;
import com.lt.bookkeeping.Fragment.FragmentMain;
import com.lt.bookkeeping.Fragment.FragmentPersonInfo;
import com.lt.bookkeeping.R;
import com.lt.bookkeeping.coustomView.TitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author LiuTong
 */
public class MainActivity extends BaseActivity {

    private final List<Fragment> fragmentList = new ArrayList<>();
    private BottomNavigationBar bottomNavigationView;
    private ViewPager viewPager;
    private TitleView titleView;
    private final String[] titleNames = new String[]{"首页", "数据统计", "个人"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setXmlLayout() {
        setContentView(R.layout.activity_main);
    }

    @Override
    public void initData() {

    }


    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    public void initView() {
        titleView = findViewById(R.id.main_top_title);
        viewPager = findViewById(R.id.main_view);
        bottomNavigationView = findViewById(R.id.main_bottom_bar);
        bottomNavigationView
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
                .setMode(BottomNavigationBar.MODE_SHIFTING)
                .setBarBackgroundColor(R.color.color_91AD70)
                .setActiveColor(R.color.color_D0104C)
                .addItem(new BottomNavigationItem(
                        getResources().getDrawable(R.drawable.main, getTheme()), titleNames[0]))
                .addItem(new BottomNavigationItem(
                        getResources().getDrawable(R.drawable.data, getTheme()), titleNames[1])
                        .setBadgeItem(new TextBadgeItem()
                                .setBackgroundColor(Color.TRANSPARENT)
                                .setText("")
                                .setTextColor(Color.RED)
                                .setGravity(Gravity.TOP | Gravity.RIGHT)))
                .addItem(new BottomNavigationItem(
                        getResources().getDrawable(R.drawable.personal, getTheme()), titleNames[2]))
                .initialise();

        FragmentMain fragmentMain = new FragmentMain();
        FragmentPersonInfo fragmentPersonInfo = new FragmentPersonInfo();
        FragmentDataStatistic fragmentDataStatistic = new FragmentDataStatistic();
        Fragment[] fragments = new Fragment[]{fragmentMain, fragmentDataStatistic, fragmentPersonInfo};
        fragmentList.addAll(Arrays.asList(fragments));
        viewPager.setCurrentItem(0);
        titleView.setTitle(titleNames[0]);

    }

    @Override
    public void initEvent() {
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(),
                FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList == null ? 0 : fragmentList.size();
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                titleView.setTitle(titleNames[position]);
                bottomNavigationView.selectTab(position);
                hideSoftKeyboard();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        bottomNavigationView.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                titleView.setTitle(titleNames[position]);
                viewPager.setCurrentItem(position, true);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });

    }
    /**
     * 隐藏软键盘
     * */
    public void hideSoftKeyboard(){
        InputMethodManager manager = (InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
        View focus = getCurrentFocus();
        manager.hideSoftInputFromWindow(
                focus == null ? null : focus.getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
    }



}